﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Data.Services.Client;
using PlanB.ShareDove.ConferenceOrganization.ConsumerLayer.WinPhoneClient.ConferenceSessionsService;

namespace PlanB.ShareDove.ConferenceOrganization.ConsumerLayer.WinPhoneClient
{

    /// <summary>
    /// Main page code behind
    /// </summary>
    public partial class MainPage : PhoneApplicationPage
    {

        //private ConferenceSessionsService.SessionContext context;

        private readonly Uri ShareDoveServiceUri = new Uri("http://enterprisetest:801/ConferenceOrganizationDataService/ConferenceOrganizationService.svc/");
        private DataServiceCollection<RestSession> sessions;

        /// <summary>
        /// Main page constructor
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        /// <summary>
        /// Handle selection changed on ListBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));

            // Reset selected index to -1 (no selection)
            MainListBox.SelectedIndex = -1;
        }

        /// <summary>
        /// Load data for the ViewModel Items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

            // Initialize the context and the binding collection 
            SessionContext context = new SessionContext(ShareDoveServiceUri);
            sessions = new DataServiceCollection<RestSession>(context);

            // Define a LINQ query that returns all customers.
            var query = from sess in context.Sessions select sess;

            // Register for the LoadCompleted event.
            sessions.LoadCompleted
                += new EventHandler<LoadCompletedEventArgs>(conferenceSessions_LoadCompleted);

            // Load the customers feed by executing the LINQ query.
            sessions.LoadAsync(query);

 
        }

        /// <summary>
        /// Load completed on customers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void conferenceSessions_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Handling for a paged data feed.
                if (sessions.Continuation != null)
                {
                    // Automatically load the next page.
                    sessions.LoadNextPartialSetAsync();
                }
                else
                {
                    // Set the data context of the list box control to the sample data.
                    //this.LayoutRoot.DataContext = sessions;
                    App.ViewModel.RestData = sessions;
                }

                if (!App.ViewModel.IsDataLoaded)
                {
                    App.ViewModel.LoadData();
                }
            }
            else
            {
                MessageBox.Show(string.Format("An error has occurred: {0}", e.Error.Message));
            }
        }
    }
}